//
// Created by 12345 on 2025/2/19.
//

#include "report_bean.h"

ReportBean &ReportBean::getInstance() {
    static ReportBean instance;
    return instance;
}

void ReportBean::parse(std::string &msg) {

    dianliang = static_cast<uint8_t>(msg[12]);

    auto forward_value = static_cast<uint8_t>(msg[13]);
    forward_back_dir = (forward_value & 0x80) >> 7;
    forward_back_speed = forward_value & 0x7f;

    auto left_right_value = static_cast<uint8_t>(msg[14]);
    left_right_dir = (left_right_value & 0x80) >> 7;
    left_right_speed = left_right_value & 0x7f;

    auto left_right_turn_value = static_cast<uint8_t>(msg[15]);
    left_right_turn_dir = (left_right_turn_value & 0x80) >> 7;
    left_right_turn_speed = left_right_turn_value & 0x7f;

    auto fuyang_angle_value = static_cast<int>((msg[16] << 8) | msg[17]);
    fuyang_dir = (fuyang_angle_value & 0x8000) >> 15;
    fuyang_angle = fuyang_angle_value & 0x7fff;

    auto henggun_value = static_cast<int>((msg[18] << 8) | msg[19]);
    henggun_dir=(henggun_value & 0x8000)>>15;
    henggun_angle=henggun_value&0x7fff;

    gait_info = static_cast<uint8_t>(msg[20]);
    base_state = static_cast<uint8_t>(msg[21]);
    charge_state = static_cast<uint8_t>(msg[22]);
    weidu = static_cast<int>((msg[23] << 24) | (msg[24] << 16) | (msg[25] << 8) | msg[26]);
    jingdu = static_cast<int>((msg[27] << 24) | (msg[28] << 16) | (msg[29] << 8) | msg[30]);
    hangxiang_angle = static_cast<int>((msg[31] << 8) | msg[32]);
    jiaoju = static_cast<uint8_t>(msg[35]);
    video1_state = static_cast<uint8_t>(msg[40]);
    tuxiang_function_state = static_cast<uint8_t>(msg[42]);
    shibie_state = static_cast<uint8_t>(msg[54]);
    huokong_state = static_cast<uint8_t>(msg[43]);
    daji_state = static_cast<uint8_t>(msg[44]);
    baoxian_state = static_cast<uint8_t>(msg[45]);
    tuobaliang_x = static_cast<int>((msg[46] << 8) | msg[47]);
    tuobaliang_y = static_cast<int>((msg[48] << 8) | msg[49]);

    mp_log_info(DEFAULT_LOGGER,"parse report dianliang:%d forward_back_dir:%d forward_back_speed:%d left_right_dir:%d left_right_speed:%d left_right_turn_dir:%d left_right_turn_speed:%d "
                               "fuyang_dir:%d fuyang_angle:%d henggun_dir:%d henggun_angle:%d gait_info:%d base_state:%d charge_state:%d weidu:%d jingdu:%d hangxiang_angle:%d "
                               "jiaoju:%d video1_state:%d tuxiang_function_state:%d shibie_state:%d huokong_state:%d daji_state:%d baoxian_state:%d tuobaliang_x:%d tuobaliang_y:%d"
            ,dianliang,forward_back_dir,forward_back_speed,left_right_dir,left_right_speed,left_right_turn_dir,left_right_turn_speed,fuyang_dir,fuyang_angle,henggun_dir,henggun_angle,gait_info,
                base_state,charge_state,weidu,jingdu,hangxiang_angle,jiaoju,video1_state,tuxiang_function_state,shibie_state,huokong_state,daji_state,baoxian_state,tuobaliang_x,tuobaliang_y);

    setDafData();

}

void ReportBean::setDafData() {

    //电池信息
    DeviceAgentModel::DeviceAgentProperty batteryP;
    Gazella::Daf::DafModel::get_instance()->get_property("battery", batteryP);
    batteryP.mutable_property_array()->Clear();
    DeviceAgentModel::DeviceAgentProperty batteryPropertyP;
    Gazella::Daf::DafModel::get_instance()->get_property("battteryProperty", batteryPropertyP);
    (*batteryPropertyP.mutable_property_struct()->mutable_members())["id"].mutable_property_int()->set_value(1);
    (*batteryPropertyP.mutable_property_struct()->mutable_members())["percent"].mutable_property_int()->set_value(dianliang);
    (*batteryPropertyP.mutable_property_struct()->mutable_members())["vol"].mutable_property_int()->set_value(0); //电压 mv
    batteryP.mutable_property_array()->add_properties()->CopyFrom(batteryPropertyP);
    Gazella::Daf::DafModel::get_instance()->set_property("battery", batteryP);



    DeviceAgentModel::DeviceAgentProperty bodyReport;
    Gazella::Daf::DafModel::get_instance()->get_property("bodyReport", bodyReport);
    (*bodyReport.mutable_property_struct()->mutable_members())["forwardBackDirection"].mutable_property_int()->set_value(forward_back_dir);
    (*bodyReport.mutable_property_struct()->mutable_members())["forwardBackSpeed"].mutable_property_int()->set_value(forward_back_speed);
    (*bodyReport.mutable_property_struct()->mutable_members())["leftRightDirection"].mutable_property_int()->set_value(left_right_dir);
    (*bodyReport.mutable_property_struct()->mutable_members())["leftRightSpeed"].mutable_property_int()->set_value(left_right_speed);
    (*bodyReport.mutable_property_struct()->mutable_members())["leftRightTurnDirection"].mutable_property_int()->set_value(left_right_turn_dir);
    (*bodyReport.mutable_property_struct()->mutable_members())["leftRightTurnSpeed"].mutable_property_int()->set_value(left_right_turn_speed);
    Gazella::Daf::DafModel::get_instance()->set_property("bodyReport", bodyReport);
    DeviceAgentModel::DeviceAgentProperty angleReport;
    Gazella::Daf::DafModel::get_instance()->get_property("angleReport", angleReport);
    (*angleReport.mutable_property_struct()->mutable_members())["pitchDirection"].mutable_property_int()->set_value(fuyang_dir);
    (*angleReport.mutable_property_struct()->mutable_members())["pitchAngle"].mutable_property_int()->set_value(fuyang_angle);
    (*angleReport.mutable_property_struct()->mutable_members())["rollDirection"].mutable_property_int()->set_value(henggun_dir);
    (*angleReport.mutable_property_struct()->mutable_members())["rollAngle"].mutable_property_int()->set_value(henggun_angle);
    Gazella::Daf::DafModel::get_instance()->set_property("angleReport", angleReport);
    DeviceAgentModel::DeviceAgentProperty gait;
    Gazella::Daf::DafModel::get_instance()->get_property("gait", gait);
    gait.mutable_property_int()->set_value(gait_info);
    Gazella::Daf::DafModel::get_instance()->set_property("gait", gait);
    DeviceAgentModel::DeviceAgentProperty baseState;
    Gazella::Daf::DafModel::get_instance()->get_property("baseState", baseState);
    baseState.mutable_property_int()->set_value(base_state);
    Gazella::Daf::DafModel::get_instance()->set_property("baseState", baseState);
    DeviceAgentModel::DeviceAgentProperty chargingState;
    Gazella::Daf::DafModel::get_instance()->get_property("chargingState", chargingState);
    chargingState.mutable_property_int()->set_value(charge_state);
    Gazella::Daf::DafModel::get_instance()->set_property("chargingState", chargingState);
    DeviceAgentModel::DeviceAgentProperty position;
    Gazella::Daf::DafModel::get_instance()->get_property("position", position);
    (*position.mutable_property_struct()->mutable_members())["longitude"].mutable_property_int()->set_value(jingdu);
    (*position.mutable_property_struct()->mutable_members())["latitude"].mutable_property_int()->set_value(weidu);
    Gazella::Daf::DafModel::get_instance()->set_property("position", position);



}
